package com.migu.spms.webapi;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.migu.spms.common.StringUtil;
import com.migu.spms.service.data.User;
import com.migu.spms.service.inf.ICreateRequireService;
import com.migu.spms.webapi.data.AvailableBaselineQueryRequest;
import com.migu.spms.webapi.data.AvailableBaselineQueryResponse;
import com.migu.spms.webapi.data.CreateRequireRequest;
import com.migu.spms.webapi.data.GeneralResponse;
import com.migu.spms.webapi.data.base.BaseLine;

/**
 * 
 * 项目名称: SPMS需求管理 包: com.migu.spms.webapi 类名称: CreateRequireController.java 类描述:
 * 新建需求控制类 创建人: fw 创建时间: 2017年6月13日 下午4:29:02
 */
// @codeCreate.restfulAPICreate.util.annotation.type.Controller(introduction =
// "新建需求")
@RestController
@RequestMapping(value = "/require", produces = "application/json;charset=UTF-8")
public class CreateRequireController extends BaseController {
	@Resource(name = "createRequireService")
	ICreateRequireService createRequireService;

	// @Method(introduction = "新建需求", requestType = CreateRequireRequest.class,
	// responseType = GeneralResponse.class)
	@RequestMapping(value = "/create")
	public String create(HttpServletRequest req, HttpServletResponse resp, String sessionId) throws Exception {
		User user = getUserBySession(req, sessionId);
		if (user == null) {
			return generateResponse(401);
		}
		// User user = new User();
		// user.setUserId(1);
		// user.setUserName("zs");
		CreateRequireRequest createRequireRequest = ParseMsg(req, CreateRequireRequest.class);
		// if (createRequireRequest.getCreatorId() == null ||
		// StringUtil.isEmpty(createRequireRequest.getCreatorName())) {
		createRequireRequest.setCreatorId(Long.valueOf(user.getUserId()));
		createRequireRequest.setCreatorName(user.getUserName());
		// }
		createRequireService.create(createRequireRequest, user);
		return generateResponse(200);

	}

	// @Method(introduction = "查询可用基线版本", requestType =
	// AvailableBaselineQueryRequest.class, responseType =
	// AvailableBaselineQueryResponse.class)
	@RequestMapping(value = "/queryAvailableBaseline")
	public String queryAvailableBaseline(HttpServletRequest req, HttpServletResponse resp, String sessionId)
			throws Exception {
		User user = getUserBySession(req, sessionId);
		if (user == null) {
			return generateResponse(401);
		}
		// User user = new User();
		// user.setUserId(1);
		// user.setUserName("zs");
		AvailableBaselineQueryRequest availableBaselineQueryRequest = ParseMsg(req,
				AvailableBaselineQueryRequest.class);
		List<BaseLine> baseLines = createRequireService.queryAvailableBaseline(availableBaselineQueryRequest);
		AvailableBaselineQueryResponse availableBaselineQueryResponse = new AvailableBaselineQueryResponse();
		availableBaselineQueryResponse.setBaseLineList(baseLines);
		return generateResponse(availableBaselineQueryResponse, 200);

	}
}
